<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BSEntity
 *
 * @author jianfeng
 */
class BSEntity extends BEntity {

    public $attributes = array();

    public function load($data = NULL, $context = NULL) {

        //call init before load, use to initialize initial data
        if (!$this instanceof BlynApp) {
            if (BRepositoryFactory::createRepository2()->getCount($this) == 0)
                $this->initBeforeLoad($data);
        }

        parent::load($data, $context);
    }

    public function getModelClass($data = NULL) {
        $class = get_class($this);
        $mClass = preg_replace("/^BS/", 'BSM', $class);
        return $mClass;
    }

    public function getPropertyToColumnMapList($data = NULL) {
        //by default, use property name as column name
        /*
          $property_list = get_object_vars($this);
          $list = array();
          foreach ($property_list as $name => $value) {
          $list[$name] = $name;
          }
          if (isset($list['id']))
          unset($list['id']);
         * 
         */

        $list['amount'] = 'amount';
        $list['name'] = 'name';
        $list['description'] = 'description';
        $list['spaceId'] = 'space_id';
        $list['appId'] = 'app_id';
        $list['creatorId'] = 'creator_id';
        $list['ownerId'] = 'owner_id';
        $list['roleId'] = 'role_id';
        $list['status'] = 'status';
        $list['createTime'] = 'create_time';
        $list['unitPrice'] = 'unit_price';
        $list['priority'] = 'priority';
        $list['timeslotId'] = 'timeslot_id';
        $list['resourceId'] = 'resource_id';
        return $list;
    }

    public function addAttribute($name, $value, $data = NULL) {
        $entityContext = new BSAttribute();
        $entityContext->name = $name;
        $entityContext->value = $value;
        $entityContext->owner_class =  get_class($this);
        $entityContext->owner_id = $this->id;
        $entity = BSAttribute::find($entityContext);
        if($entity->getId() == NULL){
            $entityContext->save();
            return $entityContext;
        }
        return FALSE;
    }

    public function getAttribute($name, $data = NULL) {
        $entityContext = new BSAttribute();
        $entityContext->name = $name;
        $entityContext->value = $value;
        $entityContext->owner_class =  get_class($this);
        $entityContext->owner_id = $this->id;
        $entity = BSAttribute::find($entityContext);
        if($entity->getId()!=NULL){
            return $entity;
        }
        return FALSE;
    }

    protected function loadAttributes($data = NULL) {
        
    }

    public function initBeforeLoad($data = NULL) {
        
    }

}
